package service

import (
	"context"

	"gitee.com/zhaokangming/tiny-ticktok/dao"
	"gitee.com/zhaokangming/tiny-ticktok/model"
	"gitee.com/zhaokangming/tiny-ticktok/pkg/app"
)

type CommentListResponse struct {
	app.Response
	CommentList []model.Comment `json:"comment_list,omitempty"`
}

func CreateComment(ctx context.Context, uid, vid int64, content string) error {
	err := dao.CreateComment(ctx, uid, vid, content)
	if err != nil {
		return err
	}

	return nil
}

func DeleteComment(ctx context.Context, uid, cid int64) error {
	err := dao.DeleteComment(ctx, uid, cid)
	if err != nil {
		return err
	}

	return nil
}

func MGetComments(ctx context.Context, uid, vid int64) (*CommentListResponse, error) {
	comments, err := dao.MGetComments(ctx, vid)
	if err != nil {
		return nil, err
	}

	res := new(CommentListResponse)
	res.CommentList = make([]model.Comment, len(comments))
	for i, v := range comments {
		res.CommentList[i].Id = int64(v.ID)
		user, err := QueryUser(ctx, uid, v.UID)
		if err != nil {
			return nil, err
		}

		res.CommentList[i].User = *user
		res.CommentList[i].Content = v.Content
		res.CommentList[i].CreateDate = v.CreatedAt.String()
	}

	return res, nil
}
